/**
*      author: Y_Yao
*      created: 2025-01-25 12:22:21
**/

#include<bits/stdc++.h>

//#define int LL
using namespace std;
using LL = long long;
const LL LLINF = 1e18;
const int INF = 0x3f3f3f3f;
const int N = 350 + 7;
const int M = 120 + 7;
int n,m;
int A[N],g[5],b[M];
int f[45][45][45][45];

void solve()
{
    cin >> n >> m;
    for(int i = 1;i <= n;i++) 
    {
        cin >> A[i];
    }

    int x;
    for(int i = 1;i <= m;i++)
    {
        cin >> x;
        g[x]++; 
    }
    f[0][0][0][0] = A[1];
    for(int a = 0;a <= g[1];a++)
    {
        for(int b = 0;b <= g[2];b++)
        {
            for(int c = 0;c <= g[3];c++)
            {
                for(int d = 0;d <= g[4];d++)
                {
                    int r = 1 + a + 2 * b + 3 * c + 4 * d;
                    if(a!=0) f[a][b][c][d] = max(f[a][b][c][d],f[a-1][b][c][d] + A[r]);
                    if(b!=0) f[a][b][c][d] = max(f[a][b][c][d],f[a][b-1][c][d] + A[r]);
                    if(c!=0) f[a][b][c][d] = max(f[a][b][c][d],f[a][b][c-1][d] + A[r]);
                    if(d!=0) f[a][b][c][d] = max(f[a][b][c][d],f[a][b][c][d-1] + A[r]);
                }
            }
        }
    } 
    cout << f[g[1]][g[2]][g[3]][g[4]] << endl;
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    int t = 1;
    //cin >> t;
    while(t--)
    {
        solve();
    }
    return 0;
} 